﻿using DataModels.Dto;
using DataModels.Dto.AuthCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Services.AuthCore;
using WeChatServices;

namespace PropertyManagementWebApi.Controllers.AuthCore
{
    public class OauthController(OauthService oauthService, IConfiguration configuration) : BaseController
    {
        protected readonly OauthService _oauthService = oauthService;
        private readonly WeChatMessageHandler _messageHandler = new WeChatMessageHandler(configuration, oauthService);

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseModel> GetUserInfoAsync()
        {
            return await _oauthService.GetUserInfo();
        }

        /// <summary>
        /// 业主微信登录注册
        /// </summary>
        /// <param name="model">微信code</param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public Task<ResponseModel> VxLoginOrRegister(VxLoginOrRegister model)
        {
            return _oauthService.VxLoginOrRegister(model.Code,model.encryptedData,model.iv);
        }


        [AllowAnonymous]
        [HttpPost]
        public Task<ResponseModel> SubmitTrial(VxLoginOrRegister model)
        {
            return _oauthService.SubmitTrial(model.Code, model.encryptedData, model.iv);
        }

        /// <summary>
        /// 业主微信登录方法
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public Task<ResponseModel> VxLogin(VxLogin model)
        {
            return _oauthService.VxLogin(model.Code);
        }

        /// <summary>
        /// 商家微信登录方法
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public Task<ResponseModel> merchantsVxLogin(VxLogin model)
        {
            return _oauthService.merchantsVxLogin(model.Code);
        }

        /// <summary>
        /// 商户微信登录注册
        /// </summary>
        /// <param name="model">微信code</param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public Task<ResponseModel> merchantsVxLoginOrRegister(VxLoginOrRegister model)
        {
            return _oauthService.merchantsVxLoginOrRegister(model.Code, model.encryptedData, model.iv);
        }

        /// <summary>
        /// 手机号验证码登录或注册
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public Task<ResponseModel> PhoneCodeLoginOrRegister(PhoneCodeLoginOrRegister model)
        {
            return _oauthService.PhoneCodeLoginOrRegister(model.Phone, model.Code, model.Password);
        }

        /// <summary>
        /// 手机号密码登录
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public Task<ResponseModel> PhonePasswordLogin(PhonePasswordLogin model)
        {
            return _oauthService.PhonePasswordLogin(model.Phone, model.Password);
        }

        /// <summary>
        /// 服务号绑定小程序
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResponseModel VxFWHBindXCX(string fwhopenid)
        {
            return _oauthService.VxFWHBindXCX(fwhopenid);
        }

        /// <summary>
        /// 设置新密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ResponseModel> SetNewPassword(SetNewPassword model)
        {
            Guid uuid = Guid.Parse(HttpContext.GetGuid());
            return _oauthService.SetNewPassword(uuid,model.Phone, model.Code, model.NewPassword);
        }

        /// <summary>
        /// 发送验证码到手机
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public Task<ResponseModel> SendCode([FromQuery] SendCode model) 
        {
            return _oauthService.SendSmsCode(model.Phone);
        }

        /// <summary>
        /// 绑定vx
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ResponseModel> BindWeChat(BindWeChat model)
        {
            Guid uuid = Guid.Parse(HttpContext.GetGuid());
            return _oauthService.BindWeChat(uuid, model.Code);
        }

        /// <summary>
        /// 绑定手机号
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ResponseModel> BindPhone(BindPhone model)
        {
            return _oauthService.BindPhone(model.Phone, model.Code);
        }

        /// <summary>
        /// 注销账号
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        //[HttpPost]
        //public Task<ResponseModel> CancelAccount(CancelAccount model)
        //{
        //    string ip = HttpContext.GetClientIP();
        //    return _oauthService.CancelAccount(model.Code, ip);
        //}



        /// <summary>
        /// 微信服务器验证接口
        /// </summary>
        [AllowAnonymous]
        [HttpGet]
        public IActionResult Get(
            [FromQuery] string signature,
            [FromQuery] string timestamp,
            [FromQuery] string nonce,
            [FromQuery] string echostr)
        {
            // 验证签名
            if (_messageHandler.ValidateSignature(signature, timestamp, nonce, echostr))
            {
                // 验证成功，返回echostr
                return Content(echostr);
            }

            // 验证失败
            return BadRequest("验证失败");
        }

        /// <summary>
        /// 微信消息接收接口
        /// </summary>
        [AllowAnonymous]
        [HttpPost()]
        public async Task<IActionResult> Get()
        {
            // 处理消息推送
            return await _messageHandler.HandleMessageAsync(HttpContext);
        }


    }
}
